package com.labor_management_system.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.labor_management_system.Entity.LaborEngineering;
import com.labor_management_system.Entity.WorkDetails;
import com.labor_management_system.Mapper.WorkDetailsMapper;
import com.labor_management_system.Service.LaborEngineeringService;
import com.labor_management_system.Mapper.LaborEngineeringMapper;
import com.labor_management_system.Utils.FileTypeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
* @author deathwing475
* @description 针对表【labor_engineering】的数据库操作Service实现
* @createDate 2024-02-27 09:54:49
*/
@Service
public class LaborEngineeringServiceImpl extends ServiceImpl<LaborEngineeringMapper, LaborEngineering>
    implements LaborEngineeringService {
    //劳务工程



    @Override
    public List<LaborEngineering> showAllengineer(Integer selectPage, Integer pageSize) {

        Page<LaborEngineering> page = new Page<>(selectPage,pageSize);
        baseMapper.selectPage(page,null);

        return page.getRecords();
    }

    @Override
    public Boolean insertEnginer(LaborEngineering laborEngineering, MultipartFile multipartFile) throws IOException {
        if(multipartFile!=null){

            String filepath = "C://Users//16683//Desktop//ppg//"+laborEngineering.getLaborEngineeringId()+"."+ FileTypeUtil.getType(multipartFile).name();
            laborEngineering.setProofOfDeclaration(filepath);

            //存入磁盘
            File saveFile = new File(filepath);
            multipartFile.transferTo(saveFile);

            //保存数据库
            int insert = baseMapper.insert(laborEngineering);

            return  true;

        }

        return  false;
    }

    @Override
    public List<LaborEngineering> selectBy(LaborEngineering laborEngineering) {
        QueryWrapper<LaborEngineering> queryWrapper = new QueryWrapper<>();

        System.out.println(laborEngineering.getEnterprisesId());

        if(laborEngineering.getEnterprisesId()!=null){
            queryWrapper.eq("enterprises_id",laborEngineering.getEnterprisesId());
        }

        if(laborEngineering.getProjectId()!=null){
            queryWrapper.eq("project_id",laborEngineering.getProjectId());
        }

        if(laborEngineering.getFilingCycle1()!=null){
            queryWrapper.ge("filing_cycle1",laborEngineering.getFilingCycle1());
        }

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean updateEngineer(LaborEngineering laborEngineering, String laborEngineeringId,MultipartFile multipartFile) throws IOException {
        if(multipartFile!=null){

            String filepath = "C://Users//16683//Desktop//ppg//"+laborEngineering.getLaborEngineeringId()+"."+ FileTypeUtil.getType(multipartFile).name();
            laborEngineering.setProofOfDeclaration(filepath);

            //存入磁盘
            File saveFile = new File(filepath);
            multipartFile.transferTo(saveFile);

            int update = 0;

            QueryWrapper<LaborEngineering> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("labor_engineering_id", laborEngineeringId);

            update = baseMapper.update(laborEngineering, queryWrapper);

            if (update != 0) {
                return true;
            }

            return false;
        }

        else {
            int row = 0;

            QueryWrapper<LaborEngineering> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("labor_engineering_id", laborEngineeringId);

            row = baseMapper.update(laborEngineering, queryWrapper);

            if (row != 0) {
                return true;
            }

            return false;
        }
    }

//    @Override
//    public void deleteEngineer(String laborEngineeringId) {
//        //删除子表
//        QueryWrapper<WorkDetails> listQueryWrapper = new QueryWrapper<>();
//        listQueryWrapper.eq("labor_engineering_id",laborEngineeringId);
//        workDetailsMapper.delete(listQueryWrapper);
//
//        //删除父表
//        QueryWrapper<LaborEngineering> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("labor_engineering_id",laborEngineeringId);
//        laborEngineeringMapper.delete(queryWrapper);
//    }
}




